rm(list = ls())
gc()
require(tidyverse)

################################################################################
##
## Date:    2024-12-22
## Author:  james.h.bisbee@vanderbilt.edu
## Purpose: This script generates SI Figure 4.
## Inputs:  /scratch/jhb362/zilinsky_2023/data/results/VIMP_ranger/VIMP_2024_outcome-.*(CUTBACKSPEND|MON|FEEL|SPEND).*-weeks.*_DumFacts-FALSE_temp-chg12.RData
##            - Variable importance results generated by NFR_vimp_prep.R
##            - Summarized on the NYU HPC into PSRM_comb_weeks_chg12.RData via NFR_data_prep.R
## Outputs: ./figures/figS4.pdf
##
################################################################################

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

sessionInfo()

load('./data/VIMP_ranger/PSRM_comb_weeks_chg12.RData')

toplot <- vimp %>%
  mutate(relVimp = vimp / predErr) %>%
  select(vars,outcome,period,n,relVimp) %>%
  group_by(vars,outcome,period) %>%
  summarise_all(list(mean = ~mean(.,na.rm=T),
                     lb = ~quantile(.,.005,na.rm=T),
                     ub = ~quantile(.,.995,na.rm=T))) %>%
  ungroup() %>%
  left_join(lookup)


pdf('./figures/figS4.pdf',width = 8,height = 6)
toplot %>%
  filter(!grepl('CRIME|DEM_|ECON_|HLTH_|Covs',vars)) %>%
  mutate(period = as.Date(period)) %>%
  filter(grepl('PARTY',vars)) %>%
  filter(!outcome %in% c('COMMHAP','SOLSATCOMP','MONWORRY','SOLSAT','ENOUGHMON')) %>%
  mutate(post = period >= as.Date('2013-01-01')) %>%
  filter(period >= as.Date('2010-01-01'),
         period <= as.Date('2016-01-01')) %>%
  left_join(lookup) %>%
  ggplot(aes(x = period,y = relVimp_mean,group = post)) + 
  geom_point() + 
  geom_smooth() + 
  theme_bw() + 
  geom_vline(xintercept = as.Date('2013-01-01'),linetype = 'dashed') + 
  facet_wrap(~labs,scales = 'free',ncol = 2) + 
  labs(x = NULL,
       y = 'Variable Importance',
       title = 'Variable Importance of Partisanship',
       subtitle = 'Egotropic questions, comparing before and after Jan 1, 2013')
dev.off()

# EOF